/**
* @class    VSJSFormulaClass
* @author   Ahmad Retha
* @date     20101219
* @version  0.02
* @license  Apache 2
* @todo sum(inf,i=1)
* @todo fix sqrt{{36 | 6}} + 768976
*/

var vsjsformula = {
	/**
	* @param {String} input The text to translate into mathematical notation
	* @return {String} The HTML representation of the input string
	*/
	translate: function(input)
	{
		//replace words/symbols/operators/HTML entities with HTML symbols
		var input = input.replace(/</g, '&lt;')
		.replace(/>/g, '&gt;')
		.replace(/\/\//g, '&divide;')
		.replace(/(&lt;&gt;|!=)/g, '&ne;')
		.replace(/  /g, ' &nbsp;')
		.replace(/&gt;=/g, '&ge;')
		.replace(/&lt;=/g, '&le;')
		.replace(/(equivalent|equiv)/gi, '&equiv;')
                .replace(/[*]/gi, '&times;')
		.replace(/===?/g, '&equiv;')
		.replace(/(therefore|there4)/gi, '&there4;')
		.replace(/bullet/gi, '&bull;')
		.replace(/dprime/gi, '&Prime;')
		.replace(/[^d&]prime/gi, '&prime;')
		.replace(/&lt;--&gt;/gi, '&hArr;')
		.replace(/--&gt;/gi, '&rArr;')
		.replace(/&lt;--/gi, '&lArr;')
		.replace(/null/gi, '&empty;')
		.replace(/angle/gi, '&ang;')
		.replace(/(supersetoreq|superseteq)/gi, '&supe;')
		.replace(/(subsetoreq|subseteq)/gi, '&sube;')
		.replace(/superset/gi, '&sup;')
		.replace(/(notsubset|nsubset)/gi, '&nsub;')
		.replace(/subset/gi, '&sub;')
		.replace(/square\s?root/gi, '&radic;')
		.replace(/(sqrt|root)/gi, '&radic;')
		.replace(/(infinity|inf)/gi, '&infin;')
		.replace(/(integral|int)/gi, '&int;')
		.replace(/(integration|integrate)/gi, '&int;')
		.replace(/sum[\s]*/gi, '&sum;')
		.replace(/(plusminus|\+_)/gi, '&plusmn;')
		.replace(/(approx|like)/gi, '&asymp;')
		.replace(/(function|func)/gi, '&#402;')
		.replace(/f\(/g, '&#402;(')
		.replace(/(degrees?|deg)/gi, '&deg;')
		.replace(/squared?/gi, '<sup>2</sup>')
		.replace(/cubed?/gi, '<sup>3</sup>')
		.replace(/pi/gi, '&#960;')
		.replace(/lambda/gi, '&#955;')
		.replace(/phi/gi, '&#966;')
		.replace(/epsilon/gi, '&#949;')
		.replace(/upsilon/gi, '&#965;')
		.replace(/psi/gi, '&#968;')
		.replace(/omega/gi, '&#937;')
		.replace(/alpha/gi, '&#945;')
		.replace(/beta/gi, '&#946;')
		.replace(/gamma/gi, '&#947;')
		.replace(/delta/gi, '&#948;')
		.replace(/zeta/gi, '&#950;')
		.replace(/theta/gi, '&#952;')
		.replace(/eta/gi, '&#951;')
		.replace(/kappa/gi, '&#954;')
		.replace(/lambda/gi, '&#955;')
		.replace(/mu/gi, '&#956;')
		.replace(/xi/gi, '&#958;')
		.replace(/rho/gi, '&#961;')
		.replace(/sigmaf/gi, '&#962;')
		.replace(/sigma/gi, '&#963;')
		.replace(/tau/gi, '&#964;')
		.replace(/chi/gi, '&#967;')
        //make square root overline work
        .replace(/([^\s]+)?&radic;\{(.*?)\}/gi, '<sup>$1</sup>&radic;<span class="ol">$2</span>')
		//make powers (superscript) and subscript work
		.replace(/([0-9a-z&;#\(\)=<>\*\-"\.\/]+)[,]([0-9a-z&;#\(\)=<>\.\*\-"+]+)/gi, '$1<sub>$2</sub>')
		.replace(/([0-9a-z&;#\(\)=<>\*\-"\.\/]+)[\^]([0-9a-z&;#\(\)=<>\.\*\-"+]+)/gi, '$1<sup>$2</sup>')
		//make forward slash division work
		.replace(/([0-9a-z&;#\(\)=<>\*\-"\.\/]*)([^< ;])[\/]([0-9a-z&;#\(\)=<>*\-"\.\/]+)/gi, '<span class="nm">$1$2</span><big>/</big><span class="dn">$3</span>');
		
        //make sum work
        input = input.replace(/([^\s]+)&lt;(.*?)\s+\|\s+(.*?)&gt;/g, '<table><tr><td><sub>$2</sub></td></tr><tr><td>$1</td></tr><tr><td><sup>$3</sup></td></tr></table>');

		//make dividers work
		while(input.match(/\{(.*?)(\s+\/\s+|\s+\|\s+)(.*?)\}/g)){
			input = input.replace(/\{(.*?)(\s+\/\s+|\s+\|\s+)(.*?)\}/g, '<table><tr><td class="u">$1</td></tr><tr><td>$3</td></tr></table>');
		}
        
		//make matrices work
		while(x = input.match(/\[(.*?)\|(.*?)\]/g)){
			var arr = x[0].split(/\|/g);
			var rowspan = arr.length;
			
			var out = '<table>';
			for(var i = 0; i < rowspan; i++){
				out += '<tr>';
				if(i==0) out += '<td class="lbracket" rowspan="' + rowspan + '">&nbsp;</td>';
				
				var vals = arr[i].split(/,/);
				for(var j = 0; j < vals.length; j++){
					out += '<td>' + vals[j].replace(/^\[/, '').replace(/\]$/, '') + '</td>';
				}
				
				if(i==0) out += '<td class="rbracket" rowspan="' + rowspan + '">&nbsp;</td>';
				out += '</tr>';
			}
			out += '</table>';
			
			input = input.replace(/\[(.*?)\|(.*?)\]/, out);
		}
		
		return input;
	}
};
